/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package matching.path;

import java.net.URI;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import matching.DataPropertyMatch;
import matching.Match;
import matching.Matching;
import matching.triple.DataPropertyTriple;
import ontology.Ontology;
import org.semanticweb.owl.apibinding.OWLManager;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLDescription;
import org.semanticweb.owl.model.OWLObjectProperty;
import org.semanticweb.owl.model.OWLOntologyCreationException;
import org.semanticweb.owl.model.OWLOntologyManager;


/**
 *
 * @author arida
 */
public class SearchPaths {

    private static Path path;
    private static ArrayList<Path> allPaths;

    public static ArrayList<Path>  run(Ontology onto, OWLClass src, OWLClass target, int depth){
            allPaths = new ArrayList<Path>();
       
            path = new Path(onto.owl, src);
            search(onto, src, target, depth);
               
        for(Path p: allPaths)System.out.println(p);
        return allPaths;
    }

    private static void search(Ontology onto, OWLClass src, OWLClass target, int depth){       

        if(depth > 0){
            System.out.println("entrou");
            for(OWLObjectProperty link: onto.getObjPropertiesOf(src.toString())){
                for (OWLDescription range : link.getRanges(onto.owl)){
                    if(range.asOWLClass().equals(target)){
                        path.addNode(new PropertyPair(range.asOWLClass(), link));
                        allPaths.add(path.clone());
                        path.removeLastNode();
                    } else {
                        path.addNode(new PropertyPair(range.asOWLClass(), link));
                        search(onto, range.asOWLClass(), target, depth-1);
                        path.removeLastNode();
                    }
                }
            }

            for (OWLDescription range : src.getSubClasses(onto.owl)){
                if(range.asOWLClass().equals(target)){
                    path.addNode(new SubsumptionPair(range.asOWLClass()));
                    allPaths.add(path.clone());
                    path.removeLastNode();
                } else {
                    path.addNode(new SubsumptionPair(range.asOWLClass()));
                    search(onto, range.asOWLClass(), target, depth-1);
                    path.removeLastNode();
                }
            }
            

        }
    }

    public static void main(String args[]) throws OWLOntologyCreationException{
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        URI uriAmazon = URI.create("file:///C:/Users/arida/Documents/NetBeansProjects/trunk/MappingTool/Ontologies/Amazon.owl");
        URI uriSales = URI.create("file:///C:/Users/arida/Documents/NetBeansProjects/trunk/MappingTool/Ontologies/Sales.owl");
        Ontology ontoAm = new Ontology(uriAmazon);
        Ontology ontoSa = new Ontology(uriSales);
        OWLClass src = ontoAm.getClass("Product");
        OWLClass contSrc = ontoAm.getClass("Recorder");
        OWLClass contTgt = ontoAm.getClass("Music");
        OWLDataProperty elSrc = ontoAm.getDataProperty("recname");
        OWLDataProperty elTgt = ontoSa.getDataProperty("recorder");
        Matching m = new Matching(ontoAm, ontoSa, "am", "sa");
       // Match match = new DataPropertyMatch(new DataPropertyTriple(elSrc, contSrc, null, ontoAm), new DataPropertyTriple(elTgt, contTgt, null, ontoSa), ontoAm, ontoSa);
        ArrayList<Path> r =  run(ontoAm, src, contSrc, 3);
        System.out.print(r);       
       
        
    }
    
}
